package cn.ljy.uem.service.impl;

import cn.ljy.authority.dao.mapper.SysUserMapper;
import cn.ljy.authority.service.impl.IBaseService;
import cn.ljy.common.exception.model.MyWebException;
import cn.ljy.uem.dao.ProjectDailyMapper;
import cn.ljy.uem.dao.ProjectDailyTypeMapper;
import cn.ljy.uem.model.ProjectDaily;
import cn.ljy.uem.model.query.ProjectDailyQuery;
import cn.ljy.uem.service.ProjectDailyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;


/**
 * @description 工程日志模块的业务层接口
 * @author  王秋林
 * @date 2023-1-16 17:45
 */
@Service
@Slf4j
public class IProjectDailyService extends IBaseService<ProjectDaily, ProjectDailyQuery> implements ProjectDailyService {

    @Autowired
    private ProjectDailyMapper mapper;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private ProjectDailyTypeMapper dailyTypeMapper;


    @Override
    protected void beforeGet(Serializable id) {
        if (mapper.findById(id) == null){
            throw new MyWebException("查找失败：找不到指定记录");
        }
    }

    @Override
    protected void afterGet(ProjectDaily projectDaily) {
        if (projectDaily.getAuthorId()!=null){
            projectDaily.setAuthor(userMapper.findById(projectDaily.getAuthorId()));
        }
        if (projectDaily.getTypeId()!=null){
            projectDaily.setType(dailyTypeMapper.findById(projectDaily.getTypeId()));
        }
    }

    @Override
    protected void beforeUpdate(ProjectDaily projectDaily) {
        if (mapper.getRecycleByDailyId(projectDaily.getId())==true){
            throw new MyWebException("操作失败：不能更改已回收工程日志");
        }
    }

    @Override
    protected void beforeSave(ProjectDaily projectDaily) {
        //添加工程日志类型 默认不弃用值
//        if (projectDaily.getRecycle() == null) {
//            projectDaily.setRecycle(false); //改为直接在mapper层设置
//        }


    }

    @Override
    protected void beforeDelete(Serializable[] ids) {
        //后续需添加
    }

    @Override
    protected void beforeFindAll(ProjectDailyQuery query) {

    }

    //行政班多表查询
    @Override
    protected void afterFindAll(List<ProjectDaily> list) {
        for (ProjectDaily projectDaily : list){
            if (projectDaily.getAuthorId()!=null){
                projectDaily.setAuthor(userMapper.findById(projectDaily.getAuthorId()));
            }
            if (projectDaily.getTypeId()!=null){
                projectDaily.setType(dailyTypeMapper.findById(projectDaily.getTypeId()));
            }
        }
    }

}
